package com.imooc.security.browser.config;

import com.imooc.security.core.properties.SecurityProperties;
import com.imooc.security.core.validate.ValidateCodeFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
 * web安全应配置类
 *
 * @author yuelinbo
 * @date 2019/2/15 16:58
 **/
@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private AuthenticationSuccessHandler imoocAuthenticationHandler;

    @Autowired
    private AuthenticationFailureHandler imoocAuthenticationFailureHandler;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserDetailsService myUserDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        ValidateCodeFilter codeFilter = new ValidateCodeFilter();
       // codeFilter.setAuthenticationFailureHandler(imoocAuthenticationFailureHandler);
        //codeFilter.setSecurityProperties(securityProperties);
        //codeFilter.afterPropertiesSet();

//        http.httpBasic()
        http.formLogin() //默认登陆方法
//                .loginPage("/html/test.html")
                .loginPage("/authentication/require")
                .loginProcessingUrl("/authentication/form")
                .successHandler(imoocAuthenticationHandler)
                .failureHandler(imoocAuthenticationFailureHandler)
                .and()
                //.rememberMe()
                //.tokenRepository(persistentTokenRepository())
               // .tokenValiditySeconds(securityProperties.getRememberMeSeconds())
               // .userDetailsService(myUserDetailsService)
               // .and()
                .authorizeRequests()
                .antMatchers("/html/test.html").permitAll()
                .antMatchers("/authentication/require").permitAll()
                .antMatchers("/code/image").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf().disable();
    }

    /**
     * 设置密码的存储格式 5.0中要求
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

   /* @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .passwordEncoder(passwordEncoder())
                .withUser("qin").password(passwordEncoder().encode("111")).roles("USER");
    }*/


   /**
    * 配置记住我功能
    *
    * @author yuelinbo
    * @date 2019/3/6 18:29
    **/
   /*@Bean
    public PersistentTokenRepository persistentTokenRepository(){

       JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
       tokenRepository.setDataSource(dataSource);
       //系统启动时自动创建 token用表
//       tokenRepository.setCreateTableOnStartup(true);
       return tokenRepository;

   }*/
}
